//
//  MJRefreshAutoFooter.swift
//  OC-Swift
//
//  Created by 陆文宇 on 2017/2/15.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class MJRefreshAutoFooter: MJRefreshFooter {
    
    override var isHidden: Bool {
        set {
            let lastHidden: Bool = super.isHidden
            
            super.isHidden = newValue
            
            if !lastHidden && newValue {
                state = .idle
                
                scrollView?.mj_insetB -= mj_h
            } else if lastHidden && !newValue {
                scrollView?.mj_insetB += mj_h
                
                mj_y = scrollView!.mj_contentH
            }
        }
        
        get {
            return super.isHidden
        }
    }
    
    var automaticallyRefresh: Bool = true
    var triggerAutomaticallyRefreshPercent: CGFloat = 1
    override var state: MJRefreshComponent.MJRefreshState {
        set {
            let oldState: MJRefreshState = super.state
            if super.state != newValue {
                super.state = newValue
            }
            
            if state == .refreshing {
                DispatchQueue.main.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds + NSEC_PER_MSEC * 500), execute: {
                    self.executeRefreshingCallback()
                })
            } else if newValue == .noMoreData || newValue == .idle {
                if oldState == .refreshing {
                    if endRefreshingCompletionClosure != nil {
                        endRefreshingCompletionClosure!()
                    }
                }
            }
        }
        
        get {
            return _state
        }
    }
    
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if newSuperview != nil {
            if isHidden == false {
                scrollView?.mj_insetB += mj_h
            }
            
            mj_y = scrollView!.mj_contentH
        } else {
            if isHidden == false {
                scrollView?.mj_insetB -= mj_h
            }
        }
    }

    override func prepare() {
        super.prepare()
    }
    
    override func scrollViewContentSizeDidChange(change: Dictionary<NSKeyValueChangeKey, Any>?) {
        super.scrollViewContentSizeDidChange(change: change)
        
        mj_y = scrollView!.mj_contentH
    }
    
    override func scrollViewContentOffsetDidChange(change: Dictionary<NSKeyValueChangeKey, Any>) {
        super.scrollViewContentOffsetDidChange(change: change)
        
        if state != .idle || !automaticallyRefresh || mj_y == 0 {
            return
        }
        
        if scrollView!.mj_insetT + scrollView!.mj_contentH > scrollView!.mj_h {
            if scrollView!.mj_offsetY >= scrollView!.mj_contentH - scrollView!.mj_h + mj_h * triggerAutomaticallyRefreshPercent + scrollView!.mj_insetB - mj_h {
                
                let old: CGPoint = change[.oldKey] as! CGPoint
                let new: CGPoint = change[.newKey] as! CGPoint
                if new.y < old.y {
                    return
                }
                
                beginRefreshing()
            }
        }
    }

    override func scrollViewPanStateDidChange(change: Dictionary<NSKeyValueChangeKey, Any>) {
        super.scrollViewPanStateDidChange(change: change)
        
        if state != .idle {
            return
        }
        
        if scrollView!.panGestureRecognizer.state == .ended {
            if scrollView!.mj_insetT + scrollView!.mj_contentH <= scrollView!.mj_h {
                if scrollView!.mj_offsetY >= -scrollView!.mj_insetT {
                    beginRefreshing()
                }
            } else {
                if scrollView!.mj_offsetY >= scrollView!.mj_contentH + scrollView!.mj_insetB - scrollView!.mj_h {
                    beginRefreshing()
                }
            }
        }
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
